package com.hmall.item.listener;

import cn.hutool.core.util.StrUtil;
import com.hmall.common.constant.RedisConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @Author whx
 * @Date 2024/11/15 11:23
 */
@Component
@Slf4j
public class RedisListener {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "preheat.queue1"),
            exchange = @Exchange(name = "preheat.exchange", type = ExchangeTypes.TOPIC),
            key = "preheat.*"
    ))
    public void preheatListListener(String msg){
        if(StrUtil.isEmpty(msg)){
           return;
        }

        redisTemplate.delete(RedisConstant.PREHEAT_LIST);
        log.info(msg + ": " +  RedisConstant.PREHEAT_LIST);

    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "preheat.queue2"),
            exchange = @Exchange(name = "preheat.exchange", type = ExchangeTypes.TOPIC),
            key = "preheat.*"
    ))
    public void preheatTotalListener(String msg){

        if(StrUtil.isEmpty(msg)){
            return;
        }

        redisTemplate.delete(RedisConstant.PREHEAT_TOTAL);
        log.info(msg + ": " +  RedisConstant.PREHEAT_TOTAL);

    }

}
